#include <iostream>
#include "missocket/Tcptomis.h"
#include "utils/RedisUtil.h"
#include <cstdlib>
#include <jsoncpp/json/json.h>
#include <vector>
#include <stdio.h>
using namespace std;

int main() {
    string ip="121.15.171.92";
    int port = 8010;
    string Info="";
    RedisUtil redisUtil;
    Tcptomis tcptomis;
    Json::Value root;
    root["Type"]="user-act";
    root["Command"]="getUserInf";
    root["Prefix"]="/d2FuZzc4OQ--";
    cout << root.toStyledString()<<endl;

    Info=tcptomis.submitEcho(root.toStyledString());

    Json::Reader Reader;
    Json::Value FirstJson;
    Json::Value MessageJson;
    Reader.parse(Info,FirstJson);
    MessageJson=FirstJson["Message"];

    Json::Reader reader;
    Json::Value SecondJson;
    reader.parse(MessageJson.toStyledString(),SecondJson);
    //string MID=SecondJson["MID"].asString();

    string username="d2FuZzc4OQ--";
    redisUtil.connect("127.0.0.1",6379);
    redisUtil.set(username,"{\"BeiDou\":\"113.981877,22.59955\",\"GPS\":\"\",\"MID\":\"867355030067797\",\"Mac\":\"A8:A6:68:D6:FF:5E\",\"SIM\":\"460000221718691\"}");
    string Fromredis=redisUtil.get(username);

    vector<double> nums;
    const char * split = ",";

    string beidou=SecondJson["BeiDou"].toStyledString();
    cout << beidou.length() <<endl;
    string beidou1=beidou.substr(1,beidou.length()-3);
    cout << beidou1 << endl;
    char *s_input = (char *)beidou1.c_str();
    char *p = strtok(s_input,split);
    double a;
    while(p != NULL)
    {
        // char * -> int
        sscanf(p, "%lf", &a);
        nums.push_back(a);
        p=strtok(NULL, split);
    }

    cout<<"输出得到的数字："<<endl;
    for(a = 0; a < nums.size(); a++)
    {
        cout<<nums[a]<<endl;
    }

    cout << Info << endl;

    cout << "For MID:" << endl << SecondJson["BeiDou"].toStyledString() << endl;

    cout << "get from redis:" << endl << Fromredis << endl;

    //cout << MID << endl;




    return 0;
}
